/* -*- Mode:Prolog; coding:ISO-8859-1; -*- */
:- ensure_loaded([list_utils]).

%@ @item get_folds(+Examples, +N, -Folds) 
%@ Partitions the examples in a list Folds of N lists. 
get_folds(Examples, N, Folds) :-
        length(Examples, L),
        Dim is integer(L/N),
        get_foldsx(Examples,Dim, Folds).
        

get_foldsx(Examples, Dim, [Sampled | Folds]) :-
        length(Examples, L),
        D2 is Dim *2,
        (L >= D2 ->
        sample_examples(Examples, Dim, Sampled, Remaining),
        get_foldsx(Remaining, Dim, Folds)
        ;
        Sampled = Examples,
        Folds = []
        ).


%@ @item sample_examples(+Examples, +N, +NumNegativeExamples, -Sampled, -Remaining) 
%@ ([example(atom,integer), ...], integer,integer, [example(atom,integer), ...],[example(atom,integer), ...]) 
%@ Returns a subset OutE of Examples, with N examples
%@ It does NOT assert the examples
sample_examples(Examples, NE, Sampled, Remaining) :-
        rnd_select(Examples, NE, Sampled, Remaining).




measures(OutTest, Performance) :-        
        findall(E1, (member(out_example(E1, Coeff1, _), OutTest), Coeff1 > 0), ListPosExamples),  
        findall(E2, (member(out_example(E2, Coeff2, _), OutTest), Coeff2 < 0), ListNegExamples),
        length(ListPosExamples, Pos),
        length(ListNegExamples, Neg),
        (findall(E3, (member(out_example(E3, Coeff3, Cl3), OutTest), Coeff3 > 0, Cl3 > 0), ListProvedPosExamples) -> true ; ListProvedPosExamples = []),
        (findall(E4, (member(out_example(E4, Coeff4, Cl4), OutTest), Coeff4 < 0, Cl4 > 0), ListProvedNegExamples) -> true ; ListProvedNegExamples = []),
        length(ListProvedPosExamples, PPos),
        length(ListProvedNegExamples, PNeg),
        %ROC analysis 
        TruePositive is PPos,
        TrueNegative is Neg - PNeg,
        FalsePositive is PNeg,
        FalseNegative is Pos - PPos,
        (Pos = 0 -> Sensitivity = nan ; Sensitivity is TruePositive/Pos), %hit-rate, recall
        FallOut is FalsePositive/Neg, %false-positive rate, false-alarm rate
        ((Temp1 is (Pos + Neg), Temp1 = 0) -> Accuracy = nan ; Accuracy is (TruePositive + TrueNegative) / (Pos + Neg)),
        Specificity is 1 - FallOut, %true-negative rate
        ((Temp2 is (TruePositive + FalsePositive), Temp2 = 0) -> Precision = nan; Precision is TruePositive / (TruePositive + FalsePositive)), %positive predictive value
%       NegativePredictiveValue = TrueNegative / (TrueNegative + FalseNegative),
        Performance = [       
                              roc(totpos, Pos),
                              roc(totneg, Neg),
                              roc(tp, TruePositive), 
                              roc(tn, TrueNegative),
                              roc(fp, FalsePositive),
                              roc(fn, FalseNegative),
                              roc(tpr, Sensitivity),
                              roc(fpr, FallOut),
                              roc(acc, Accuracy),
                              roc(spc, Specificity),
                              roc(ppv, Precision)
                       ]. 


